from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from orders.models import OrderInfo, OrderGoods
from sadmin.serializersss.order_serializers import OrderInfoSerializers, OrderInfosSerializers
from sadmin.utils import Mypage

class OrderInfoViewSet(ModelViewSet):
    queryset = OrderInfo.objects.all()
    serializer_class = OrderInfoSerializers
    pagination_class = Mypage
    def get_queryset(self):
        data = self.request.query_params.get('keyword')
        if data:
            return self.queryset.filter(order_id__contains=data)
        return OrderInfo.objects.all()

    def get_orders(self,request,pk):
        queryset = OrderInfo.objects.filter(pk=pk)
        serializer_class = OrderInfosSerializers(queryset[0])
        return Response(serializer_class.data)

    def patch_data(self,request,pk):
        data = request.data.get('status')

        queryset = OrderInfo.objects.filter(pk=pk)
        serializer_class = OrderInfosSerializers(queryset[0],data={'status':data},partial=True)
        serializer_class.is_valid(raise_exception=True)
        serializer_class.save()
        return Response(serializer_class.data)